package com.enterpriseDigitalPlatform.controller.renshi.attendanceMonth;

import com.enterpriseDigitalPlatform.dto.PageRes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class AttendanceMonthDao {
    private final JdbcTemplate jdbcTemplate;

    @Autowired
    public AttendanceMonthDao(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public int editData(AttendanceMonthDTO attendanceMonthDTO) {
        ArrayList<String> keyArr = new ArrayList<String>();
        ArrayList<Object> valueArr = new ArrayList<Object>();
        if (attendanceMonthDTO.getRequiredDays() != null) {
            keyArr.add("required_days = ?");
            valueArr.add(attendanceMonthDTO.getRequiredDays());
        }
        if (attendanceMonthDTO.getActualDays() != null) {
            keyArr.add("actual_days = ?");
            valueArr.add(attendanceMonthDTO.getActualDays());
        }
        valueArr.add(attendanceMonthDTO.getId());
        if (!keyArr.isEmpty()) {
            String sql = "update renshi_attendance_month set " + String.join(",", keyArr) + " where id = ?";
            return jdbcTemplate.update(sql, valueArr.toArray());
        }
        return 0;
    }

    public PageRes<AttendanceMonth> getList(AttendanceMonthDTO attendanceMonthDTO) {
        ArrayList<String> keyArr = new ArrayList<String>();
        ArrayList<Object> valueArr = new ArrayList<Object>();
        if (attendanceMonthDTO.getId() != null) {
            keyArr.add("id = ?");
            valueArr.add(attendanceMonthDTO.getId());
        }
        if (attendanceMonthDTO.getStaffId() != null) {
            keyArr.add("staff_id = ?");
            valueArr.add(attendanceMonthDTO.getStaffId());
        }
        if (attendanceMonthDTO.getDeptId() != null) {
            keyArr.add("dept_id = ?");
            valueArr.add(attendanceMonthDTO.getDeptId());
        }
        if (attendanceMonthDTO.getWorkMonth() != null) {
            keyArr.add("work_month = ?");
            valueArr.add(attendanceMonthDTO.getWorkMonth());
        }
        if (attendanceMonthDTO.getType() != null) {
            keyArr.add("type = ?");
            valueArr.add(attendanceMonthDTO.getType());
        }
        String sql = "select * from renshi_attendance_month ";
        String sqlCount = "select count(*) from renshi_attendance_month ";
        if (!keyArr.isEmpty()) {
            sql += "where " + String.join(" and ", keyArr);
            sqlCount += "where " + String.join(" and ", keyArr);
        }
        Long total = jdbcTemplate.queryForObject(sqlCount, Long.class, valueArr.toArray());
        if (attendanceMonthDTO.getPage() != null && attendanceMonthDTO.getPageSize() != null) {
            keyArr.add(" order by id desc limit ?,?");
            valueArr.add((attendanceMonthDTO.getPage() - 1) * attendanceMonthDTO.getPageSize());
            valueArr.add(attendanceMonthDTO.getPageSize());
        }
        List<AttendanceMonth> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(AttendanceMonth.class), valueArr.toArray());
        return new PageRes<>(attendanceMonthDTO.getPage(), attendanceMonthDTO.getPageSize(), total, list);
    }
}
